<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="药品名称" prop="medicineName">
        <el-autocomplete
          v-model="queryParams.medicineName"
          clearable
          :fetch-suggestions="querySearchAsync"
          placeholder="请输入药品名称"
          @select="handleSelect"
        ></el-autocomplete>
      </el-form-item>
      <el-form-item label="所在药房" prop="medicineHouse">
        <el-input
          v-model="queryParams.medicineHouse"
          placeholder="请输入所在药房"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
        >搜索
        </el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
        >重置
        </el-button
        >
      </el-form-item>
    </el-form>

    <el-table
      v-loading="loading"
      :data="medicineStorageList"
      @selection-change="handleSelectionChange"
      border
    >
      <el-table-column label="序号" type="index" width="50"></el-table-column>
      <el-table-column label="药品名称" align="center" prop="medicineName"/>
      <el-table-column label="所在药房" align="center" prop="medicineHouse"/>
      <el-table-column
        label="医保类型"
        align="center"
        prop="medicalInsuranceDesc"
      />
      <el-table-column label="是否基药" align="center" prop="isBaseMedicine"/>
      <el-table-column label="集采批次" align="center" prop="purchaseBatch"/>
      <el-table-column label="药品规格" align="center" prop="medicineSpec"/>
      <el-table-column label="计量单位" align="center" prop="meterUnit"/>
      <el-table-column label="生产厂家" align="center" prop="productFactory"/>
      <el-table-column label="零售价格" align="center" prop="medicinePrice"/>
      <el-table-column label="库存数量" align="center" prop="medicineAmount"/>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改药品库存对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="药品名称" prop="medicineName">
          <el-input v-model="form.medicineName" placeholder="请输入药品名称"/>
        </el-form-item>
        <el-form-item label="所在药房" prop="medicineHouse">
          <el-input v-model="form.medicineHouse" placeholder="请输入所在药房"/>
        </el-form-item>
        <el-form-item label="医保类型" prop="medicalInsuranceDesc">
          <el-input
            v-model="form.medicalInsuranceDesc"
            placeholder="请输入医保类型"
          />
        </el-form-item>
        <el-form-item label="是否基药" prop="isBaseMedicine">
          <el-input
            v-model="form.isBaseMedicine"
            placeholder="请输入是否基药"
          />
        </el-form-item>
        <el-form-item label="集采批次" prop="purchaseBatch">
          <el-input v-model="form.purchaseBatch" placeholder="请输入集采批次"/>
        </el-form-item>
        <el-form-item label="药品规格" prop="medicineSpec">
          <el-input v-model="form.medicineSpec" placeholder="请输入药品规格"/>
        </el-form-item>
        <el-form-item label="计量单位" prop="meterUnit">
          <el-input v-model="form.meterUnit" placeholder="请输入计量单位"/>
        </el-form-item>
        <el-form-item label="生产厂家" prop="productFactory">
          <el-input
            v-model="form.productFactory"
            placeholder="请输入生产厂家"
          />
        </el-form-item>
        <el-form-item label="零售价格" prop="medicinePrice">
          <el-input v-model="form.medicinePrice" placeholder="请输入零售价格"/>
        </el-form-item>
        <el-form-item label="库存数量" prop="medicineAmount">
          <el-input
            v-model="form.medicineAmount"
            placeholder="请输入库存数量"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm"
        >确 定
        </el-button
        >
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  search,
  listMedicineStorage,
  getMedicineStorage,
  delMedicineStorage,
  addMedicineStorage,
  updateMedicineStorage
} from '@/api/workstation/medicineStorage'

export default {
  name: 'MedicineStorage',
  data() {
    return {
      dlist: [
        'A',
        'B',
        'C',
        'D',
        'E',
        'F',
        'G',
        'H',
        'I',
        'J',
        'K',
        'L',
        'M',
        'N',
        'O',
        'P',
        'Q',
        'R',
        'S',
        'T',
        'U',
        'V',
        'W',
        'X',
        'Y',
        'Z'
      ],
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 药品库存表格数据
      medicineStorageList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        medicineName: undefined,
        medicineHouse: undefined,
        medicalInsuranceDesc: undefined,
        isBaseMedicine: undefined,
        purchaseBatch: undefined,
        medicineSpec: undefined,
        meterUnit: undefined,
        productFactory: undefined,
        medicinePrice: undefined,
        medicineAmount: undefined,
        medicineFirstSpell: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        // id: [{ required: true, message: "库存ID不能为空", trigger: "blur" }],
      }
    }
  },
  created() {
    this.getList()
  },
  watch: {
  },
  methods: {
    strClick(item, index) {
      this.queryParams.medicineFirstSpell = item
      this.getList()
    },
    querySearchAsync(queryString, cb) {
      let results = []
      if (!queryString) {
        cb(results)
      } else {
        //这里去查询后端的接口.根据自己接口的情况进行赋值
        search(queryString).then((res) => {
          if (res.code == 200) {
            let resArr = res.data
            //循环放到一个远程搜索需要的数组
            resArr.forEach((item) => {
              results.push({
                value: item.medicineName,
                id: item.id,
                medicineFirstSpell: item.medicineFirstSpell
              })
            })
            cb(results)
          } else {
            results = []
            cb(results)
          }
        })
      }
    },
    //点击出现搜索后点击的每一项
    handleSelect(item) {
      console.log(item)
      this.queryParams.medicineName = item.value
    },
    /** 查询药品库存列表 */
    getList() {
      this.loading = true
      listMedicineStorage(this.queryParams).then((response) => {
        this.medicineStorageList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        medicineName: undefined,
        medicineHouse: undefined,
        medicalInsuranceDesc: undefined,
        isBaseMedicine: undefined,
        purchaseBatch: undefined,
        medicineSpec: undefined,
        meterUnit: undefined,
        productFactory: undefined,
        medicinePrice: undefined,
        medicineAmount: undefined,
        medicineFirstSpell: undefined,
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      // 药品首字母重置
      this.queryParams.medicineFirstSpell = ''
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加药品库存'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.loading = true
      this.reset()
      const id = row.id || this.ids
      getMedicineStorage(id).then((response) => {
        this.loading = false
        this.form = response.data
        this.open = true
        this.title = '修改药品库存'
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.buttonLoading = true
          if (this.form.id != null) {
            updateMedicineStorage(this.form)
              .then((response) => {
                this.$modal.msgSuccess('修改成功')
                this.open = false
                this.getList()
              })
              .finally(() => {
                this.buttonLoading = false
              })
          } else {
            addMedicineStorage(this.form)
              .then((response) => {
                this.$modal.msgSuccess('新增成功')
                this.open = false
                this.getList()
              })
              .finally(() => {
                this.buttonLoading = false
              })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal
        .confirm('是否确认删除药品库存编号为"' + ids + '"的数据项？')
        .then(() => {
          this.loading = true
          return delMedicineStorage(ids)
        })
        .then(() => {
          this.loading = false
          this.getList()
          this.$modal.msgSuccess('删除成功')
        })
        .catch(() => {
        })
        .finally(() => {
          this.loading = false
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        'workstation/medicineStorage/export',
        {
          ...this.queryParams
        },
        `medicineStorage_${new Date().getTime()}.xlsx`
      )
    }
  }
}
</script>
